#!/bin/bash
# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.
# mysql 主从状态监控 正常:2  异常：非2
#
MYSQLD=`ps -e | grep -v grep |grep -c   mysqld_safe`

if [ ${MYSQLD} -lt 1 ]; then 
    exit 0
fi

function getMysqlStatus(){
    VALUE=`HOME=${MYSQL_HOME_PATH} mysqladmin ping 2> /dev/null | grep -c alive`
    echo ${VALUE}
}

if [[ $(getMysqlStatus) = "1" ]]; then
    HOME=${MYSQL_HOME_PATH} mysql 2> /dev/null -e 'show slave status\G'|grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes
fi